package flc.ast.fragment;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.youth.banner.indicator.RectangleIndicator;
import com.youth.banner.listener.OnPageChangeListener;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.CardTemplateActivity;
import flc.ast.adapter.MyBannerAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.view.container.StkEventContainer;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private List<String> listShow = new ArrayList<>();
    private MyBannerAdapter bannerAdapter;
    private Dialog myDeleteDialog;
    private String selPath;


    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivMake.setOnClickListener(this);

        bannerAdapter = new MyBannerAdapter(new ArrayList<>(), mContext);

        mDataBinding.banner.setAdapter(bannerAdapter, true);//false不无限循环
        mDataBinding.banner.setBannerGalleryEffect(0, 30, 60, 1);
        mDataBinding.banner.isAutoLoop(false);//false不自动轮播
        bannerAdapter.setmListener(new MyBannerAdapter.OnItemClickListener() {
            @Override
            public void onClick(String path, int position) {

            }

            @Override
            public void onClickDelete(String path, int position) {
                selPath = path;
                myDeleteDialog.show();
            }

            @Override
            public void onClickDownload(String path, int position) {
                saveImg(path);
            }
        });
        mDataBinding.banner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                bannerAdapter.setSelPosition(position);
                bannerAdapter.notifyDataSetChanged();
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //删除弹框
        deleteDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivMake://制作邀请卡
                startActivity(CardTemplateActivity.class);
                break;
            //删除弹框
            case R.id.ivDialogDeleteCancel://取消
                myDeleteDialog.dismiss();
                break;
            case R.id.ivDialogDeleteRight://确认
                delete(selPath);
                myDeleteDialog.dismiss();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.myCardRecord;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getPath());
            }
        }
        if (listShow.size() > 0) {
            bannerAdapter.setSelPosition(0);
            bannerAdapter.setDatas(listShow);
            bannerAdapter.notifyDataSetChanged();
            mDataBinding.banner.setVisibility(View.VISIBLE);
            mDataBinding.tvCardNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.banner.setVisibility(View.GONE);
            mDataBinding.tvCardNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除
    private void delete(String path) {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        FileUtils.delete(path);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        getData();
                        ToastUtils.showShort(R.string.delete_suc);
                    }
                });
            }
        }, 1000);
    }

    //保存图片
    private void saveImg(String path) {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        FileP2pUtil.copyPrivateImgToPublic(mContext, path);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_to_album);
                    }
                });
            }
        }, 1000);
    }

    //删除弹框
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.CENTER);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        lp.width = (int) (dm.widthPixels * 1);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogCancel = inflate.findViewById(R.id.ivDialogDeleteCancel);
        ImageView dialogRight = inflate.findViewById(R.id.ivDialogDeleteRight);
        dialogCancel.setOnClickListener(this);
        dialogRight.setOnClickListener(this);
    }

}